<?php

namespace phptools;

use \Fabula, \Gtk;

require_once(PHPTOOLS_PATH . "/interfaces/frmObfuscator.interface.php");
require_once(PHPTOOLS_PATH . "/library/phptrasher.class.php"); 
require_once(PHPTOOLS_PATH . "/library/Compiler.class.php");

/**
 * Classe de ações do formulario frmObfuscator
 *
 * @name frmObfuscator
 */
class frmObfuscator extends IfrmObfuscator {
	/**
	 * @name __construct()
	 */
	public function __construct() {
		parent::__construct();
	}
	
	/**
	 * Método do carregamento do formulario
	 * 
	 * @name frmObfuscator_onload()
	 */
	public function frmObfuscator_onload() {
		// Inicia a aplicação
		$this->widgets['frmObfuscator']->show_all();
		Gtk::main();
	}
	
	/**
	 * Método do descarregamento do formulario
	 * 
	 * @name frmObfuscator_unload()
	 */
	public function frmObfuscator_unload() {
		// Encerra a aplicação
		Gtk::main_quit();
	}
	
	/**
	 * Método do evento click do toolbar
	 * 
	 * @name tlbMain_onclick
	 * @param mixed $button Objeto do botão, comunmente um GtkToolButton
	 * @param integer $index Index do botão pressionado
	 */
	public function tlbMain_onclick($button, $index) {
		switch($index) {
			case 1:
				$this->loadFiles();
				break;
			case 2:
				$model = $this->widgets['trvFiles']->get_model();
				$rows = $model->iter_n_children(NULL);
				for($row=0; $row<$rows; $row++) {
					$iter = $model->get_iter($row);
					$convert = $model->set($iter, 0, FALSE);
				}
				break;
			case 3:
				$model = $this->widgets['trvFiles']->get_model();
				$rows = $model->iter_n_children(NULL);
				for($row=0; $row<$rows; $row++) {
					$iter = $model->get_iter($row);
					$convert = $model->set($iter, 0, TRUE);
				}
				break;
			case 4:
				$this->convertFiles();
				break;
		}
	}
	
	/**
	 * Obfusca os arquivos
	 */
	public function convertFiles() {
		$model = $this->widgets['trvFiles']->get_model();
		$rows = $model->iter_n_children(NULL);
		for($row=0; $row<$rows; $row++) {
			$iter = $model->get_iter($row);
			$convert = $model->get_value($iter, 0);
			var_dump($convert);
			if($convert === TRUE) {
				$file = $model->get_value($iter, 2);
				
				// Include the phptrasher class 
				
				$phptrasher = new phptrasher();
				$phptrasher->initialize();
				$phptrasher->removecomments = true; 
				$phptrasher->removelinebreaks = true; 
				$phptrasher->obfuscateclass = false; 
				$phptrasher->obfuscatefunction = true; 
				$phptrasher->obfuscatevariable = true; 
				$obfuscated = $phptrasher->trash($file); 
				file_put_contents($file, $obfuscated);


				// Instantiate the compiler object 
				
				$Compiler = new Compiler(); 
				$Compiler->OpenPHPTag = "<?php ";
				$Compiler->ClosePHPTag = "";
				$Output = $Compiler->Compile($file, $file);
				//file_put_contents("Action.o.php", $Output);

				
			}
		}
	}
	
	/**
	 * Carrega os arquivos do diretório escolhido
	 */
	 public function loadFiles() {
		$file = \Fabula::GtkFileChooserDialog("Abrir", $this->widgets['frmObfuscator'], FALSE, \Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
		$dirs = $file->run();
		$path = $dirs[0];
		
		// Busca arquivos PHP dentro do diretório recursivamente
		$files = $this->readDir($path, $path);
	 }
	 
	 /**
	  * Carrega os arquivos recursivamente a partir de um diretório
	  */
	 public function readDir($path, $initial_path) {
		 $files = scandir($path);
		 foreach($files as $file) {
			 \Fabula::doEvents();
			 if(($file == ".") || ($file == "..")) {
				 continue;
			 }
			 elseif(is_dir($path . "/" . $file)) {
				 $this->readDir($path . "/" . $file, $initial_path);
			 }
			 else {
				 if(substr($file, strrpos($file, ".")) == ".php") {
					 $filename = str_replace($initial_path, "", $path . "/" . $file);
					 $this->widgets['trvFiles']->add_row(array(TRUE, $filename, $path . "/" . $file));
				 }
			 }
		 }
	 }
}
